function disable_btns(){
    $("#btn_upload_1").attr("disabled",true);
    $("#btn_handle_1").attr("disabled",true);
    $("#btn_download_1").attr("disabled",true);
    $("#btn_upload_2").attr("disabled",true);
    $("#btn_handle_2").attr("disabled",true);
    $("#btn_download_2").attr("disabled",true);
    $("#btn_upload_3").attr("disabled",true);
    $("#btn_handle_3").attr("disabled",true);
    $("#btn_download_3").attr("disabled",true);
}
function able_btns(){
    $("#btn_upload_1").removeAttr("disabled");
    $("#btn_handle_1").removeAttr("disabled");
    $("#btn_download_1").removeAttr("disabled");
    $("#btn_upload_2").removeAttr("disabled");
    $("#btn_handle_2").removeAttr("disabled");
    $("#btn_download_2").removeAttr("disabled");
    $("#btn_upload_3").removeAttr("disabled");
    $("#btn_handle_3").removeAttr("disabled");
    $("#btn_download_3").removeAttr("disabled");
}

function fileChange(type){
    $("#fileTypeError_"+type).html('');
    var formData = new FormData();
    var name = $("input").val();
    

    var fileName = $('#file_upload_'+type).val();//获得文件名称
    formData.append("file",$("#file_upload_"+type)[0].files[0]);
    formData.append("fileName",fileName);
    formData.append("type",type);
    
    
    var fileType = fileName.substr(fileName.length-4,fileName.length);//截取文件类型,如(.xls)
    
    if(fileType=='.csv'){//验证文件类型
        $.ajax({
            url: '/Reg/ajax_uploadFile',//上传地址
            type: 'POST',
            cache: false,
            data: formData,//表单数据
            processData: false,
            contentType: false,
            beforeSend:function(){
                disable_btns();
                $("#fileTypeError_"+type).empty();
                $("#fileTypeError_"+type).html('正在进行，请稍候');
            },
            success:function(d){
                console.log(d.status);
                able_btns();
                if(d.status==1){
                    
                    if(d.data.status==1){
                        $("#fileTypeError_"+type).empty();
                        $("#fileTypeError_"+type).html('上传成功');
                        $("#lab_upload_"+type).empty();
                        $("#lab_upload_"+type).html(d.data.uploaded_name);
                    }else{
                        $("#fileTypeError_"+type).empty();
                        $("#fileTypeError_"+type).html(d.data.info);
                        $("#lab_upload_"+type).empty();
                    }
                }else{
                    $("#fileTypeError_"+type).empty();
                    $("#fileTypeError_"+type).html('上传失败');
                    $("#lab_upload_"+type).empty();
                }
            },
            error:function(){
                able_btns();
                $("#fileTypeError_"+type).empty();
                $("#fileTypeError_"+type).html('处理失败');
                $("#lab_upload_"+type).empty();
            }
        });
    }else{
        $("#fileTypeError_"+type).html('*上传文件类型错误,支持类型: .csv');
    }
}



$('#btn_handle_1,#btn_handle_2').click(function(){
    var url_handle = '';
    if($(this).data('type')==1){
        url_handle = '/Reg/ajax_register_batch';//个人批量开户
    }else{
        url_handle = '/Reg/ajax_eInfo_register';//企业信息批量写入
    }
    var type = $(this).data('type');
    $.ajax({
            async:false,
            url: url_handle,
            type: 'post',
            dataType: 'json',
            data: { 'uploaded': $('#lab_upload_'+type).text(),'type': type,'_token':$('input[name=_token]').val()},
            beforeSend:function(){
                disable_btns();
                $("#fileTypeError_"+type).empty();
                $("#fileTypeError_"+type).html('正在进行，请稍候');
            },
            success: function (d) {
                    able_btns();
                    if(d.status){
                        if(d.data.status==1){
                            $("#fileTypeError_"+type).empty();
                            $("#fileTypeError_"+type).html(d.data.info);
                            console.log(d);
                            $("#lab_download_"+type).empty();
                            $("#lab_download_"+type).html(d.data.downloaded);
                        }else{
                            $("#fileTypeError_"+type).empty();
                            $("#fileTypeError_"+type).html(d.data.info);
                            $("#lab_download_"+type).empty();
                        }
                        
                    }else{
                        $("#fileTypeError_"+type).empty();
                        $("#fileTypeError_"+type).html('处理失败');
                        $("#lab_download_"+type).empty();
                    }
                },
            error:function(){
                able_btns();
                $("#fileTypeError_"+type).empty();
                $("#fileTypeError_"+type).html('处理失败');
                $("#lab_download_"+type).empty();
            }
            });
});

$('#btn_handle_3').click(function(){
    var url_handle = '/Reg/ajax_batch_res';
    var type = $(this).data('type');
    var reg_type = $("input[name='reg_type']:checked").val();

    $.ajax({
            async:false,
            url: url_handle,
            type: 'post',
            dataType: 'json',
            data: { 'uploaded': $('#lab_upload_'+type).text(),'reg_type': reg_type,'type': type,'_token':$('input[name=_token]').val()},
            beforeSend:function(){
                disable_btns();
                $("#fileTypeError_"+type).empty();
                $("#fileTypeError_"+type).html('正在进行，请稍候');
            },
            success: function (d) {
                able_btns();
                    if(d.status){
                        if(d.data.status==1){
                            $("#fileTypeError_"+type).empty();
                            $("#fileTypeError_"+type).html(d.data.info);
                        }else{
                            $("#fileTypeError_"+type).empty();
                            $("#fileTypeError_"+type).html('处理失败');
                        }
                    }else{
                        $("#fileTypeError_"+type).empty();
                        $("#fileTypeError_"+type).html('处理失败');
                    }
                },
            error:function(){
                able_btns();
                $("#fileTypeError_"+type).empty();
                $("#fileTypeError_"+type).html('处理失败');
            }
            });
});

$('#btn_download_1,#btn_download_2').click(function(){
    var type = $(this).data('type');
    if($('#lab_download_'+type).text()!=''){
        window.location.href = '/Reg/ajax_getDownload/?filename='+$('#lab_download_'+type).text()+'&type='+type;
    }
    
});

function model_download(filename,type){
    window.location.href = '/Reg/ajax_getDownload/?filename='+filename+'&type='+type;
}







